
$(document).ready(function(){
	/**
	 * This runs when the page is loaded
	 */
	loadSidebar();    
    $('#id_pace').change(function(){submitPace();});
    $('#id_rations').change(function(){submitRations();});
    if ($('#message').length != 0){
		$( "#message" ).dialog({
			height: 140,
			modal: true
		});
    }
});

function setUpSeperator(objMain,objSeparator,objSidebar){
	/**
	 * @param objMain the jQuery object that has the content and the sidebar
	 * @param objSeparator the jQuery object that has the vertical separator
	 * @param objSidebar the jQuery object that has the Sidebar info to be shown then hidden 
	 * Sets up the vertical Separator by binding hide and show 
	 * setting up what to do on hover and setting the default image
	 */
	objSeparator.click(function(e){
        e.preventDefault();
        if ( objMain.hasClass('use-sidebar') ){
            hideSidebar(objMain,objSidebar);
        }
        else {
            showSidebar(objMain,objSidebar);
        }
    }).css('height', objSeparator.parent().outerHeight() + 'px');
	
	objSeparator.hover(function() {
		$(this).css('background','');
		$(this).addClass('ui-state-highlight');	  
	  	}, 
	  	function() {
	  		$(this).css('background',
	  				'url("' + STATIC_URL + 'images/independent/vertical-separator.gif") repeat-y center top');
	  		$(this).removeClass('ui-state-highlight');
	  	}
	);
	//This is the line you see and the code that makes it repeat vertically
	objSeparator.css('background',
				'url("' + STATIC_URL + 'images/independent/vertical-separator.gif") repeat-y center top');
}

function sidebarByCookie(objMain){
	/**
	 * @param objMain the jQuery object that has the content and the sidebar
	 * Allows user to keep sidebar preference in a cookie 
	 * This uses the jQuery cookie plugin
	 */
    if ( $.cookie('sidebar-pref2') == null ){
        objMain.removeClass('use-sidebar');
    }
}

function loadSidebar(){
	/**
	 * Sets up the sidebar and initalizes the variables that hold important info
	 */
    var objMain = $('#main');
    // Sidebar separator
    var objSeparator = $('#separator'); 
    //the div to show and hide 
    var objSidebar = $('#sidebar');
    
    setUpSeperator(objMain,objSeparator,objSidebar);    
    sidebarByCookie(objMain);
}


function showSidebar(objMain,objSidebar){
	/**
	 * @param objMain the jQuery object that has the content and the sidebar
	 * @param objSidebar the jQuery object that has the Sidebar info to be shown then hidden 
	 * Shows the sidebar and sets the cookie to use-sidebar
	 */
	objSidebar.hide().fadeIn();
    objMain.addClass('use-sidebar');
    $.cookie('sidebar-pref2', 'use-sidebar', { expires: 30 });
}

function submitPace(){
	$.ajax({url:$('#changePaceForm').attr('url'),
		type: "GET",
		data: $('#changePaceForm').serialize(),
		dataType: 'html',
		success:function(info){
			$.notifyBar({
				html: 'Successfully changed pace to "' + paceOptions[info] + '"',
				delay: 1000,
				animationSpeed: "normal",
				cls: "success"
			});
		},
		error:function(){
			$.notifyBar({
				html: 'Unable to change pace',
				delay: 1000,
				animationSpeed: "normal",
				cls: "error"
			});
		}
	});
}

function submitRations(){
	$.ajax({url:$('#changeRationsForm').attr('url'),
		type: "GET",
		data: $('#changeRationsForm').serialize(),
		dataType: 'html',
		success:function(info){
			$.notifyBar({
				html: 'Successfully changed rations to "' + rationsOptions[info] + '"',
				delay: 2000,
				animationSpeed: "normal",
				cls: "success"
			});
		},
		error:function(){
			$.notifyBar({
				html: 'Unable to change rations',
				delay: 2000,
				animationSpeed: "normal",
				cls: "error"
			});
		}
	});
}


function hideSidebar(objMain,objSidebar){
	/**
	 * @param objMain the jQuery object that has the content and the sidebar
	 * @param objSidebar the jQuery object that has the Sidebar info to be shown then hidden 
	 * Hides the sidebar and sets the cookie to null
	 */
	objSidebar.fadeOut();
	objMain.removeClass('use-sidebar', 100);
    $.cookie('sidebar-pref2', null, { expires: 30 });    
}